package epay18

import (
	"encoding/json"
	"github.com/zeromicro/go-zero/core/logx"
	"io"
	"net/http"

	"navi-mumbai/app/api/pay/internal/logic/epay18"
	"navi-mumbai/app/api/pay/internal/svc"
	"navi-mumbai/app/api/pay/internal/types"
)

func WithdrawNotifyEpay18Handler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.WithdrawNotifyEpay18Req
		defer func() {
			w.WriteHeader(http.StatusOK)
			_, err2 := w.Write([]byte("SUCCESS"))
			if err2 != nil {
				logx.WithContext(r.Context()).Errorf("[API-ERR] WithdrawNotifyEpay18Handler Write: %+v ", err2)
				return
			}
		}()

		body, err := io.ReadAll(r.Body)
		defer r.Body.Close()
		if err != nil {
			logx.WithContext(r.Context()).Errorf("WithdrawNotifyEpay18Handler ReadAll err:%+v query:%s", err, r.URL.RawQuery)
			return
		}

		err = json.Unmarshal(body, &req)
		if err != nil {
			logx.WithContext(r.Context()).Errorf("WithdrawNotifyEpay18Handler Unmarshal err:%+v body:%s", err, string(body))
			return
		}

		l := epay18.NewWithdrawNotifyEpay18Logic(r.Context(), svcCtx)
		_, _ = l.WithdrawNotifyEpay18(&req)
	}
}
